﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class AUDIT_LINE_EDIT : Form
    {
        public bool isAdd = true;
        public string reportId = string.Empty;
        private AUDIT_TASK audit_line;

        public AUDIT_LINE_EDIT(AUDIT_TASK act_form, string id, string taskId)
        {
            InitializeComponent();
            audit_line = act_form;
            if (!string.IsNullOrEmpty(id))
            {
                reportId = id;
                isAdd = false;
                BindDevice();
                BindReport(reportId, taskId);
            }
        }

        private void BindDevice()
        {
            var line = DBClass.DBHelper.GetAuditDevice("");
            if (line != null)
            {
                cmb_device.DisplayMember = "DEVICE_ID";
                cmb_device.ValueMember = "DEVICE_NAME";
                cmb_device.DataSource = line;
            }
        }

        private void BindReport(string id,string taskId)
        {   
            var line = DBClass.DBHelper.GetAuditLineByID(id).FirstOrDefault();
            if (line != null)
            {
                cmb_device.Text = line.DEVICE_NAME.ToString();
                txt_name.Text = line.TASK_NAME.ToString();               
                txt_id.Text = line.ID.ToString();
                txt_taskid.Text = taskId;
            }
        }   

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {            
            string strDevice = cmb_device.Text.Trim();
            string strTask = txt_task.Text.Trim();
            string strDeviceId = cmb_device.SelectedValue.ToString();
            string strDeviceType = string.Empty;
            if (!string.IsNullOrEmpty(strDeviceId))
            {
                strDeviceType = DBClass.DBHelper.GetAuditDeviceByDeviceID(strDeviceId).FirstOrDefault().DEVICE_TYPE_NAME;
            }
            if (string.IsNullOrEmpty(strDevice))
            {
                MessageBox.Show("请选择巡检点位！");
                return;
            }

            try
            {
                string sql = string.Empty;              
                DBEntity.Models.AUDIT_LINE line = new DBEntity.Models.AUDIT_LINE();
                if (!isAdd)
                {
                    line.ID = Int64.Parse(txt_id.Text);
                    line.TASK_ID =Int64.Parse( txt_taskid.Text);
                    line.TASK_NAME = strTask;
                    line.DEVICE_ID = strDeviceId;
                    line.DEVICE_NAME = strDevice;
                    line.DEVICE_TYPE_NAME = strDeviceType;

                    bool result = DBClass.DBHelper.UpdateAuditLine(line);
                    if (result )
                    {    
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_line.btn_query_Click(null, null);
                    }
                }
                else
                {
                    line.TASK_ID = Int64.Parse(txt_taskid.Text);
                    line.TASK_NAME = strTask;
                    line.DEVICE_ID = strDeviceId;
                    line.DEVICE_NAME = strDevice;
                    line.DEVICE_TYPE_NAME = strDeviceType;
                    long id = DBClass.DBHelper.InsertAuditLine(line);
                    if (id > 0)
                    {     
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_line.btn_query_Click(null, null);
                    }                            
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DBClass.Log.InsertLog("保存巡检路线失败！"+ex.Message);  
            }
        }
    }
}
